package com.yic.module.device.service.inspectionPlanDevice;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.device.controller.admin.device.vo.DeviceExportReqVO;
import com.yic.module.device.controller.admin.device.vo.DeviceSimpleRespVO;
import com.yic.module.device.dal.dataobject.device.DeviceDO;
import com.yic.module.device.service.device.DeviceService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.yic.module.device.controller.admin.inspectionPlanDevice.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlanDevice.InspectionPlanDeviceDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.inspectionPlanDevice.InspectionPlanDeviceConvert;
import com.yic.module.device.dal.mysql.inspectionPlanDevice.InspectionPlanDeviceMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 巡检计划与设备关联 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectionPlanDeviceServiceImpl implements InspectionPlanDeviceService {

    @Resource
    private InspectionPlanDeviceMapper inspectionPlanDeviceMapper;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Resource
    private DeviceService deviceService;

    @Override
    public Long createInspectionPlanDevice(InspectionPlanDeviceCreateReqVO createReqVO) {
        // 插入
        InspectionPlanDeviceDO inspectionPlanDevice = InspectionPlanDeviceConvert.INSTANCE.convert(createReqVO);
        inspectionPlanDeviceMapper.insert(inspectionPlanDevice);
        // 返回
        return inspectionPlanDevice.getId();
    }

    @Override
    public void updateInspectionPlanDevice(InspectionPlanDeviceUpdateReqVO updateReqVO) {
        // 校验存在
        validateInspectionPlanDeviceExists(updateReqVO.getId());
        // 更新
        InspectionPlanDeviceDO updateObj = InspectionPlanDeviceConvert.INSTANCE.convert(updateReqVO);
        inspectionPlanDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectionPlanDevice(Long id) {
        // 校验存在
        validateInspectionPlanDeviceExists(id);
        // 删除
        inspectionPlanDeviceMapper.deleteById(id);
    }

    private void validateInspectionPlanDeviceExists(Long id) {
        if (inspectionPlanDeviceMapper.selectById(id) == null) {
            throw exception(INSPECTION_PLAN_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public InspectionPlanDeviceDO getInspectionPlanDevice(Long id) {
        return inspectionPlanDeviceMapper.selectById(id);
    }

    @Override
    public List<InspectionPlanDeviceDO> getInspectionPlanDeviceList(Collection<Long> ids) {
        return inspectionPlanDeviceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectionPlanDeviceDO> getInspectionPlanDevicePage(InspectionPlanDevicePageReqVO pageReqVO) {
        return inspectionPlanDeviceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectionPlanDeviceDO> getInspectionPlanDeviceList(InspectionPlanDeviceExportReqVO exportReqVO) {
        return inspectionPlanDeviceMapper.selectList(exportReqVO);
    }

    @Override
    public void createPlanDevice(Long planId, List<DeviceSimpleRespVO> deviceList) {
        List<InspectionPlanDeviceCreateReqVO> list = new ArrayList<>();
        list = deviceList.stream().map(planDevice -> {
            InspectionPlanDeviceCreateReqVO inspectionPlanDevice = new InspectionPlanDeviceCreateReqVO();
            inspectionPlanDevice.setPlanId(planId);
            inspectionPlanDevice.setDeviceId(planDevice.getId());
            return inspectionPlanDevice;
        }).collect(Collectors.toList());
        List<InspectionPlanDeviceDO> inspectionPlanDeviceList = InspectionPlanDeviceConvert.INSTANCE.convert03(list);
        transactionTemplate.execute(status -> {
            inspectionPlanDeviceMapper.deleteByPlanId(planId);
            inspectionPlanDeviceMapper.insertBatch(inspectionPlanDeviceList);
            return Boolean.TRUE;
        });
    }

    @Override
    public List<InspectionPlanDeviceDO> selectListByPlanId(Long planId) {
        return inspectionPlanDeviceMapper.selectListByPlanId(planId);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        inspectionPlanDeviceMapper.deleteByPlanId(planId);
    }

    @Override
    public PageResult<InspectionPlanDeviceDO> getInspectionPlanDeviceAppPage(InspectionPlanDevicePageReqVO pageReqVO) {
        List<Long> deviceIdList = new ArrayList<>();
        if (StrUtil.isNotBlank(pageReqVO.getCode())) {
            DeviceExportReqVO deviceExportReqVO = new DeviceExportReqVO();
            deviceExportReqVO.setCode(pageReqVO.getCode());
            List<DeviceDO> deviceDOList = deviceService.getList(deviceExportReqVO);
            if (CollectionUtil.isNotEmpty(deviceDOList)) {
               deviceIdList = deviceDOList.stream().map(DeviceDO::getId).collect(Collectors.toList());
            }
        }
        pageReqVO.setDeviceIdList(deviceIdList);
        return inspectionPlanDeviceMapper.selectAppPage(pageReqVO);
    }
}
